#include "TcpServer.hpp"
#include "Http.hpp"
#include "Log.hpp"
#include <memory>

using namespace log_ns;

HttpResponse login(HttpRequest& req)
{
    HttpResponse resp;
    std::cout<<"login Get Args"<<std::endl;
    req.GetBodyText();
    std::cout<<"-----------------------------------------------------------"<<std::endl;
    // .....
    resp.AddCode(200, "OK");
    resp.AddBodyText("<html><h1>hello Linux</h1></html>");

    // pipe
    // dup2
    // fork
    // exec*

    return resp;
}

int main(int argc, char *args[])
{
    if (argc != 2)
    {
        LOG(FATAL, "Usage: ./tcpserver -localport\n");
        exit(0);
    }
    uint16_t port = std::stoi(args[1]);
    
    HttpServer httpserver;
    httpserver.InsertServerList("/login", login);
    /* 
    std::unique_ptr<TcpServer> tsvr = std::make_unique<TcpServer>(
        std::bind(&HttpServer::HttpHandlerServer, &httpserver, std::placeholders::_1), 
        port
    );
    */
   
    std::unique_ptr<TcpServer> tsvr = std::make_unique<TcpServer>(
        std::bind(&HttpServer::HandlerServer, &httpserver, std::placeholders::_1), 
        port
    );
    tsvr->Loop();
    return 0;
}
